package com.zyx.hadoopdemo.hdfsdemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.net.URI;

/**
 * @author Yaxi.Zhang
 * @since 2021/5/19 13:25
 * desc: 获取配置了HA的HDFS连接
 * 在CDH中以下配置内容可以在core-site.xml以及hdfs-site.xml文件中查找
 *      find /run/cloudera-scm-agent/process -name core-site.xml
 *      find /run/cloudera-scm-agent/process -name hdfs-site.xml
 */
public class HdfsHaTest {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        // 参数设置
        conf.set("fs.defaultFS","hdfs://FuyuanNameservice1");
        conf.set("dfs.nameservices", "FuyuanNameservice1");

        // nn1,nn2不要求与配置文件中完全一致， 比如我查出来的配置文件中是 namenode53,namenode55
        // 但是nn1以及nn2对应的namenode的rpc地址必须正确
        conf.set("dfs.ha.namenodes.FuyuanNameservice1", "nn1,nn2");
        conf.set("dfs.namenode.rpc-address.FuyuanNameservice1.nn1", "bigdata021:8020");
        conf.set("dfs.namenode.rpc-address.FuyuanNameservice1.nn2", "bigdata023:8020");

        conf.set("dfs.client.failover.proxy.provider.FuyuanNameservice1", "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://FuyuanNameservice1/test/checkpoint_test"), conf);

        // RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fs.listFiles(new Path("/tmp"), false);
        // while (locatedFileStatusRemoteIterator.hasNext()) {
        //     System.out.println(locatedFileStatusRemoteIterator.next());
        // }
        System.out.println(fs);
        fs.close();
    }
}
